/*
AttoPilot Current and Voltage Sensing Demo

Physical Connections:
-------------------------
Arduino  | Peripherals
-------- | --------------
TX1 ------ SerLCD "RX"
5V ------- SerLCD "VCC"
GND ------ SerLCD "GND"

Pin A0 --- AttoPilot "V"
Pin A1 --- AttoPilot "I"
GND ------ AttoPilot "GND"
*/


  int VRaw;     //This will store our raw ADC data
  int IRaw;
  float VFinal; //This will store the converted data
  float IFinal;

void setup() {
    
  Serial1.begin(9600);
  backlightSerLcd(70);
  clearSerLcd();
  Serial1.print("Muthanna 2012!!");
  delay(2000);
}


void loop() { 

  //Measurement
  VRaw = analogRead(A0);
  IRaw = analogRead(A1);
 
  clearSerLcd();
  
  //Conversion
  //VFinal = VRaw/49.44;   //  45 Amp board
  VFinal = VRaw/12.99;     //  90 Amp board
  //VFinal = VRaw/12.99;   //  180 Amp board  
  
  //IFinal = IRaw/14.9;    //  45 Amp board
  IFinal = IRaw/7.4;       //  90 Amp board
  //IFinal = IRaw/3.7;     //  180 Amp board
    
  Serial1.print(VFinal);
  Serial1.print(" Volts.");
  line2SerLcd();
  Serial1.print(IFinal);
  Serial1.print(" Amps.");

  delay(200);
}

/*----------------------------------------------------------------------------------------------*/
void clearSerLcd(){
  Serial1.write(0xFE);                              //command flag
  Serial1.write(0x01);                              //clear command.
  delay(50);
}
/*----------------------------------------------------------------------------------------------*/
void backlightSerLcd(int thePercentage){            //turns on the backlight
  Serial1.write(0x7C);                              //command flag for backlight stuff
  int theValue = map(thePercentage, 0,100,128,157); // maps percentage to what SerLCD wants to see
  Serial1.write(theValue);                          //light level.
  delay(50);
}
/*----------------------------------------------------------------------------------------------*/
void line2SerLcd(){
  Serial1.write(0xFE);                              //command flag
  Serial1.write(0xC0);                              //move cursor to first location in 2nd line.
  delay(50);
}
/*----------------------------------------------------------------------------------------------*/
